$.extend({
	getUrlVars: function(){
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++){
			
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
	getUrlVar: function(name){
		return $.getUrlVars()[name];
	}
});

function updateTips( tips, t ) {
	tips
		.text( t )
		.removeClass().addClass( "ui-state-highlight" );
	setTimeout(function() {
		tips.removeClass( "ui-state-highlight", 1500 );
	}, 800 );
}

function checkLength( tips, o, n, min, max ) {
	if ( (o.val().length > max) || (o.val().length < min )) {
		o.addClass( "ui-state-error" );
		updateTips( tips, "La taille de " + n + " doit etre entre " +
			min + " et " + max + "." );
		return false;
	} else {
		return true;
	}
}

function checkRegexp( tips, o, regexp, n ) {
	if ( !( regexp.test( o.val() ) ) ) {
		o.addClass( "ui-state-error" );
		updateTips( tips, n );
		return false;
	} else {
		return true;
	}
}

$(function() {

	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	$( "#register" )
	.button()
	.click(function() {
		$.ajax({
			   type: "GET",
			   url: "/ASOF/register.htm",
			   error:function(msg){
			     alert( "Error ! " );
			   },
			   success:function(data){
				   $( "#register-dialog" ).append(data);
				   $( "#register-dialog" ).dialog( "open" );
				   $('.registerInput').keypress(function(e) {
				        if(e.which == 13) {
				            jQuery(this).blur();
				            jQuery('#submit-register').focus().click();
				        }
				    });
			   }
		});			   
		
	});
	
	$( "#selectPerson" )
	.button()
	.click(function() {
		$.ajax({
			   type: "GET",
			   url: "/ASOF/selectPerson.htm",
			   error:function(msg){
			     alert( "Error ! " );
			   },
			   success:function(data){
				   $( "#selectPerson-dialog" ).append(data);
				   $( "#selectPerson-dialog" ).dialog( "open" );
				   $('.selectPersonInput').keypress(function(e) {
				        if(e.which == 13) {
				            jQuery(this).blur();
				            jQuery('#submit-selectPerson').focus().click();
				        }
				    });
			   }
		});			   
		
	});
	
	$( "#login" )
	.button()
	.click(function(){
		$.ajax({
			   type: "GET",
			   url: "/ASOF/login.htm",
			   error:function(msg){
			     alert( "Error ! " );
			   },
			   success:function(data){
				   $( "#login-dialog" ).append(data);
				   $( "#login-dialog" ).dialog( "open" );
				   $('.loginInput').keypress(function(e) {
				        if(e.which == 13) {
				            jQuery(this).blur();
				            jQuery('#submit-login').focus().click();
				        }
				    });
			   }	   
		});
			
	});
	
	$( "#confirmation-logout-dialog" ).dialog({
		autoOpen: false,
		height: 300,
		minHeight: 300,
		width: 380,
		minWidth: 380,
		modal: true,
		close: function() {
			$( "#confirmation-logout-dialog" ).children().remove();
		},
		buttons: [{
				id:"Cancel",
				tabindex:0,
				text:"annuler",
				click: function() {
							$( this ).dialog( "close" );
							$( "#confirmation-logout-dialog" ).children().remove();
						}
			},{
				id:"confim",
				tabindex:1,
				text:"Confirmer",
				click: function() {
							$( this ).dialog( "close" );
							$( "#confirmation-logout-dialog" ).children().remove();
							window.location.replace("/ASOF/logout.htm");
						}
			}
			
		]
	});
	
	$( "#logout" )
	.button()
	.click(function(){
		$( "#confirmation-logout-dialog" ).append("<p>vous voulez vraiment vous déconnecter?</p>");
		$( "#confirmation-logout-dialog" ).dialog("open");
			
	});
	
		
	
	$( "#changePwd" )
	.button()
	.click(function(data){
		$.ajax({
			type: "GET",
			url: "/ASOF/changePwd.htm",
			error:function(msg){
				alert( "Error ! " );
			},
			success:function(data){
				$( "#changePwd-dialog" ).append(data);
				$( "#changePwd-dialog" ).dialog( "open" );
				$('.changePwdInput').keypress(function(e) {
			        if(e.which == 13) {
			            jQuery(this).blur();
			            jQuery('#submit-changePwd').focus().click();
			        }
			    });
			}
		});
	});
	
	$("#bugReport")
	.button()
	.click(function(data){
		$.ajax({
			type: "GET",
			url: "/ASOF/bugReport.htm",
			error:function(msg){
				alert( "Error ! " );
			},
			success:function(data){
				$( "#bug-report-dialog" ).append(data);
				$( "#bug-report-dialog" ).dialog( "open" );
				
			}
		});
	});
	
	$( "#notification-reload-dialog" ).dialog({
		autoOpen: false,
		height: 300,
		minHeight: 300,
		width: 380,
		minWidth: 380,
		modal: true,
		close: function() {
			$( "#notification-reload-dialog" ).children().remove();
			location.reload();
		},
		buttons: [{
				id:"fermer",
				tabindex:0,
				text:"ok",
				click: function() {
							$( this ).dialog( "close" );
							$( "#notification-reload-dialog" ).children().remove();
							location.reload();
						
						}
			}
		]
	});
	
	$( "#notification-dialog" ).dialog({
		autoOpen: false,
		height: 300,
		minHeight: 300,
		width: 380,
		minWidth: 380,
		modal: true,
		close: function() {
			$( "#notification-dialog" ).children().remove();
		},
		buttons: [{
				id:"fermer",
				tabindex:0,
				text:"ok",
				click: function() {
							$( this ).dialog( "close" );
							$( "#notification-dialog" ).children().remove();
						}
			}
		]
	});
	
	
	
    if($.getUrlVar('login')=='open')
    	$("#login").click();

    if($.getUrlVar('notification')=='open')
    	$( "#notification-dialog" ).dialog("open");
		
	
});